package Calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame {
    //创建了一个名为 Calculator 的类，它继承自 JFrame
    private JTextField textField;
    private double result = 0;
    private String operator = "";
    private boolean startOfNumber = true;

    public Calculator() {
        setTitle("多功能计算器");
        textField = new JTextField("0", 12);
        textField.setEditable(false);
        textField.setHorizontalAlignment(JTextField.RIGHT);

        add(textField, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        //定义了一个 JTextField 来显示计算结果，以及一个 JPanel 来放置按钮。
        // 按钮的文本来自 buttonLabels 数组，
        panel.setLayout(new GridLayout(4, 4, 5, 5));

        String[] buttonLabels = {//创建按钮 buttonLabels 数组
                "7", "8", "9", "/",
                "4", "5", "6", "*",
                "1", "2", "3", "-",
                "0", ".", "C", "=",
                "sin", "cos", "tan", "sqrt"
        };


        //每个按钮都添加了一个 CalculatorActionListener 监听器。
        for (String label : buttonLabels) {
            JButton button = new JButton(label);
            panel.add(button);
            button.addActionListener(new CalculatorActionListener());
        }

        add(panel, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null); // 将计算器居中显示在屏幕上
    }


    //CalculatorActionListener 类实现了 ActionListener 接口，并在 actionPerformed 方法中处理按钮点击事件。
    private class CalculatorActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();

            if ('0' <= command.charAt(0) && command.charAt(0) <= '9' || command.equals(".")) {
                if (startOfNumber) {
                    textField.setText(command);
                    startOfNumber = false;
                } else {
                    textField.setText(textField.getText() + command);
                }
            } else if (command.equals("C")) {
                textField.setText("0");
                operator = "";
                result = 0;
                startOfNumber = true;
            } else if (command.equals("=")) {
                double secondNumber = Double.parseDouble(textField.getText());
                calculate(secondNumber);
                operator = "";
                startOfNumber = true;
            } else {
                double secondNumber = Double.parseDouble(textField.getText());
                operator = command;
                result = secondNumber;
                startOfNumber = true;
            }
        }
    }

    //calculate 方法根据操作符执行基本的数学运算。我们使用了 Math 包中的函数来执行三角函数和平方根计算。
    private void calculate(double secondNumber) {
        switch (operator) {
            case "+": result += secondNumber; break;
            case "-": result -= secondNumber; break;
            case "*": result *= secondNumber; break;
            case "/": result /= secondNumber; break;
            case "sin": result = Math.sin(Math.toRadians(result)); break;
            case "cos": result = Math.cos(Math.toRadians(result)); break;
            case "tan": result = Math.tan(Math.toRadians(result)); break;
            case "sqrt": result = Math.sqrt(result); break;
        }
        textField.setText("" + result);
    }

    public static void main(String[] args) {

        Calculator calculator = new Calculator();
        calculator.setVisible(true);
    }
}